package com.whynot.frame.ms.controller;

import com.whynot.frame.basic.common.model.APIBase;
import com.whynot.frame.basic.mybatis.conroller.BaseTableController;
import com.whynot.frame.ms.entities.SysMenu;
import com.whynot.frame.ms.entities.SysRole;
import com.whynot.frame.ms.entities.SysUser;
import com.whynot.frame.ms.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 描    述:用户基础接口
 * 创建时间: 2018/06/30
 */
@Api(value = "user",description = "用户接口")
@RequestMapping("/user")
@RestController
public class UserController extends BaseTableController<UserServiceImpl,SysUser>{

    @ApiOperation(value = "获取当前用户角色接口", notes = "获取当前用户角色接口", response = APIBase.class)
    @PostMapping("/findRolesByUserId")
    public APIBase findRolesByUserId() {
        APIBase result = new APIBase();
        try {
            result.setData(this.service.findRolesByUserId());
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    @ApiOperation(value = "获取当前用户权限接口", notes = "获取当前用户权限接口", response = APIBase.class)
    @PostMapping("/findPermissionsByUserId")
    public APIBase findPermissionsByUserId(){
        APIBase result = new APIBase();
        try {
            result.setData(this.service.findPermissionsByUserId());
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    @ApiOperation(value = "修改密码", notes = "修改密码", response = APIBase.class)
    @PostMapping("/updatePwd")
    public APIBase updatePwd(
            @ApiParam(name = "oldPwd",value = "旧密码",required = true) @RequestParam("oldPwd") String oldPwd,
            @ApiParam(name = "newPwd",value = "新密码",required = true) @RequestParam("newPwd") String newPwd){
        APIBase result = new APIBase();
        try {
            result.setData(this.service.updatePwd(oldPwd,newPwd));
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    @ApiOperation(value = "为用户添加角色", notes = "为用户添加角色", response = APIBase.class)
    @PostMapping("/insertUserRole")
    public APIBase insertUserRole(
            @ApiParam(name = "userId",value = "用户Id",required = true) @RequestParam("userId") String userId,
            @ApiParam(name = "roleIds",value = "角色Id集合，用,号分割",required = true) @RequestParam("roleIds") String roleIds){
        APIBase result = new APIBase();
        try {
            result.setData(this.service.insertUserRole(userId,roleIds));
        }catch (Exception e){
            result.setMsg(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }



}